//
//  TagBlackNumber.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/10/10.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "TagBlackNumber.h"
#import "TagNumberCell.h"
#import "TagInfo.h"
#import "AddBlacklist.h"
#import "LewPopupViewController.h"
#import "NterceptionCell.h"
#import "DbManage.h"
#import "HttpTool.h"
#import "RunSucessInfo.h"
#import "AddMark.h"
#import "LewPopupViewController.h"
#import "BindTool.h"
@interface TagBlackNumber ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *dataArr;
    NSString *user_id;
    MarkInfo *markInfo;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation TagBlackNumber

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    [super setTitleView:self.view withName:@"标记的黑名单号码"];
    _table.delegate =self;
    _table.dataSource =self;
    dataArr =[NSMutableArray array];
    [dataArr addObjectsFromArray:[[DbManage shareDbManage] queryMarkArray]];
    user_id =[BindTool share].bindInfo.phone_number;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NterceptionCell *cell =[NterceptionCell setNterceptionCellWithTableView:tableView];
    cell.markInfo= [dataArr objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //弹出一个对话框
   markInfo =[dataArr objectAtIndex:indexPath.row];
    __weak typeof(self) weakSelf = self;
    AddMark *mark =[AddMark headerView];
    [weakSelf lew_presentPopupView:mark animation:[LewPopupViewAnimationFade new] dismissed:^{
    }];
    mark.closeblock = ^(){
        //在这里另外找个对话框
        [weakSelf lew_dismissPopupView];
        [weakSelf showAlert];
    };
    mark.cancleTagBlock = ^(){
        NSLog(@"cancleTagBlock=====");
        [weakSelf delteMarkToServer:markInfo];
        [weakSelf lew_dismissPopupView];
    };
    mark.cancleblock = ^(){
        [weakSelf lew_dismissPopupView];
    };
    mark.okTagBlock=^(NSString *showText){
        [weakSelf lew_dismissPopupView];
        markInfo.mark =showText;
        [weakSelf updateMarkToServer:markInfo];
    };
}
- (void)deletItem:(NSIndexPath *)indexPath{
    AddBlacklist *mark =[AddBlacklist headerView];
    mark.selcect=YES;
    [self lew_presentPopupView:mark animation:[LewPopupViewAnimationDrop new] dismissed:^{
        NSLog(@"动画结束");
    }];
}
-(void)updateMarkToServer:(MarkInfo *)info{
     __weak typeof(self) weakSelf = self;
    NSDictionary *pa =@{@"user_id":user_id,@"number":info.number,@"mark":info.mark,@"time":info.time,@"data":@""};
    NSString *url =[apiUrl stringByAppendingString:saveMark];
    [HttpTool Post:url parameters:pa success:^(id responseObject) {
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"updateMarkToServer====%@",str);
        RunSucessInfo *success =[RunSucessInfo mj_objectWithKeyValues:responseObject];
        if (success.status) {
            //插入数据库中
            NSMutableArray *array =[NSMutableArray array];
            [array addObject:info];
            [[DbManage  shareDbManage] updateMarkInfo:array];
            [weakSelf.table reloadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)delteMarkToServer:(MarkInfo *)info{
    __weak typeof(self) weakSelf = self;
    NSLog(@"这个怎么回事啊");
    NSString *url =[apiUrl stringByAppendingString:deleteMark];
    NSLog(@"delteMarkToServer====%@",url);
    NSDictionary *pa =@{@"user_id":user_id,@"number":info.number};
    [HttpTool GET:url WithHeader:NO parameters:pa success:^(id responseObject) {
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"delteMarkToServer====%@",str);
        RunSucessInfo *success =[RunSucessInfo mj_objectWithKeyValues:responseObject];
        if (success.status) {
            //插入数据库中
            NSMutableArray *array =[NSMutableArray array];
            [array addObject:info];
            [[DbManage  shareDbManage] deleteMarkArray:array];
            //删除，然后开始进来的时候去获取所有的mark,同步到服务器中去
            [dataArr removeObject:info];
            [weakSelf.table reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"failure==d=ad=a=da=da=");
    }];
}
-(void)showAlert{
    __weak typeof(self) weakSelf = self;
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定义" message:nil preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *textaa =[alertController.textFields firstObject].text;
        if (textaa!=nil) {
            markInfo.mark=textaa;
            [weakSelf updateMarkToServer:markInfo];//修改标记
        }
    }]];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
    }];
    [alertController.textFields firstObject].placeholder=@"自定义";
    [self presentViewController:alertController animated:YES completion:nil];
}
@end
